package com.kwum;

import java.text.ParseException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.commons.lang.StringUtils;

public class TimeUtils {
	
	public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");  
	public static DateTimeFormatter formatterDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");  
	public static DateTimeFormatter formatterDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");  
	public static DateTimeFormatter formatterTime = DateTimeFormatter.ofPattern("HH:mm:ss");
	/**
	 * 
	 * @Title: getNow
	 * @Description: 获取当前时间
	 * @return
	 * @throws Exception
	 * @returnType: String
	 * @author: lin
	 * @date: Jul 6, 2017 10:39:20 AM
	 */
	public static String getNow(){
		return LocalDateTime.now().format(formatterDateTime);
	}
	/**
	 * 
	 * @Title: operationNowMinute
	 * @Description: 操作当前时间的分钟数
	 * @param type 可选 : add || reduce
	 * @param num 操作的分钟数
	 * @return
	 * @throws Exception
	 * @returnType: String
	 * @author: lin
	 * @date: Jul 7, 2017 12:00:56 PM
	 */
	public static String operationNowMinute(String type, Integer num)throws Exception{
		LocalDateTime now = LocalDateTime.now();
		if(type.equals("add")){
			return now.plusMinutes(num).format(formatter);
		}
		if(type.equals("reduce")){
			return now.minusMinutes(num).format(formatter);
		}
		return null;
	}
	public static String operationNowData(Integer num)throws Exception{
		LocalDateTime now = LocalDateTime.now();
		return now.plusDays(num).format(formatterDateTime);
	}
	/**
	 * 
	 * @Title: isBefore
	 * @Description: 当前时间是否在给定时间之前
	 * @param time
	 * @return
	 * @returnType: String
	 * @author: lin
	 * @throws Exception 
	 * @date: Jul 10, 2017 2:51:25 PM
	 */
	public static boolean isBefore(String time) throws Exception{
		if(time == null){
			return false;
		}
		return LocalDateTime.now().isBefore(LocalDateTime.parse(time, formatter));
	}
	
	/**
	 * 
	 * Description:当前时间是否在给定时间之前
	 * @author feng
	 * @date 2017年12月5日 下午2:24:34
	 * @version 1.0
	 * @param time
	 * @return
	 */
	public static boolean isBeforeTime(String time){
		if(time == null){
			return false;
		}
		return LocalDateTime.now().isBefore(LocalDateTime.parse(time, formatterDateTime));
	}
	
	/**
	 * 
	 * @Title: operationNowMinute
	 * @Description: 操作当前时间的分钟数
	 * @param type 可选 : add || reduce
	 * @param num 操作的分钟数
	 * @return
	 * @throws Exception
	 * @returnType: String
	 * @author: lin
	 * @date: Jul 7, 2017 12:00:56 PM
	 */
	public static String operationNowMinute(Boolean add, Integer num,String date){
		LocalDateTime now = LocalDateTime.now();
		if(StringUtils.isNotEmpty(date)){
			now = LocalDateTime.parse(date, formatterDateTime);
		}
		if(add){
			return now.plusMinutes(num).format(formatterDateTime);
		}else{
			return now.minusMinutes(num).format(formatterDateTime);
		}
	}
	
	
	/**
	 * Description:获取当前日期
	 * @author feng
	 * @date 2017年12月6日 下午1:45:10
	 * @version 1.0
	 * @return
	 */
	public static String getNowDate(){
		return LocalDate.now().format(formatterDate);
	}
	/***
	 * 
	 * Description:两时间间隔多少分钟
	 * @author feng
	 * @date 2017年12月6日 下午2:07:28
	 * @version 1.0
	 * @param start
	 * @param end
	 * @return
	 */
	public static long getBetween(String start,String end){
		LocalDateTime startTime = LocalDateTime.parse(start,formatterDateTime);
		LocalDateTime eindTime = LocalDateTime.parse(end,formatterDateTime);
		return Duration.between(startTime, eindTime).toMinutes();
	}
	
	/***
	 * 
	 * Description:操作秒数
	 * @author feng
	 * @date 2017年12月20日 上午9:51:55
	 * @version 1.0
	 * @param time
	 * @param seconds
	 * @return
	 */
	public static String operatingSeconds(String time,int seconds){
		LocalDateTime operating = LocalDateTime.parse(time,formatterDateTime);
		return  operating.plusSeconds(seconds).format(formatterDateTime);
	}
	
	/**
	 * 对日期进行操作
	 * @param num 日期加减
	 * @return eg:2017-12-27
	 * @throws Exception
	 * @author kwum
	 */
	public static String operateDay(Integer num)throws Exception{
        LocalDateTime now = LocalDateTime.now();
        return now.plusDays(num).format(formatterDate);
    }
	
	/**
	 * 时间戳转时间
	 * @param timestamp
	 * @return
	 * @author Kwum
	 */
	public static String timestamp2String(long timestamp) {
		return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(timestamp * 1000));
	}
	
	/**
	 * 时间转时间戳
	 * @param time
	 * @return
	 * @author Kwum
	 * @throws ParseException 
	 */
	public static long string2Timestamp(String time) throws ParseException {
		return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time).getTime() / 1000;
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println(string2Timestamp("2018-02-11 01:00:00"));
		System.out.println(timestamp2String(1518278400));
		System.out.println(1518282000 > 1518278400);
    }
}
